package com.mjlf.compute;
import java.awt.*;
import java.awt.event.ActionEvent;
import javax.swing.*;

public class View
{
    public static String[] buttonVaule = {
            "1", "2", "3", "/",
            "4", "5", "6", "-",
            "7", "8", "9", "*",
            "(", "0", ")", "="
    };

    public static JFrame frame=new JFrame("按键计算器");

    //创建GridBagLayout布局管理器
    public  static GridBagLayout gbaglayout=new GridBagLayout();

    public static Double result = null;

    public static JButton[] buttons = new JButton[16];

    public static JTextArea textArea = new JTextArea();

    public static Boolean isCleanGreen = false;

    public static JButton makeButton(String title,JFrame frame,GridBagLayout gridBagLayout,GridBagConstraints constraints)
    {
        //创建Button对象
        JButton button=new JButton(title);
        button.setBackground(new Color(222, 222, 222));
        addActionListener(button);
        gridBagLayout.setConstraints(button,constraints);
        frame.add(button);
        return button;
    }

    public static void addActionListener(JButton jButton){
        String str = jButton.getText();
        jButton.addActionListener(new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String expression = textArea.getText();
                if(str.equals("=")){
                    if(isCleanGreen){
                        return;
                    }
                    result = Calculator.conversion(expression);
                    textArea.setText(expression +"=\n"+ result);
                    isCleanGreen = true;
                }else {
                    if(isCleanGreen){
                        expression = "";
                        isCleanGreen = false;
                    }
                    textArea.setText(expression + str);
                }
            }
        });
    }

    public static void doClick(int i){
        if(i > 16){
            return;
        }
        for(JButton jButton : buttons){
            jButton.setBackground(new Color(222, 222, 222));
        }

        buttons[i].setBackground(new Color(233, 201, 21));
        buttons[i].doClick();

        frame.repaint();
    }

    public static void view()
    {


        GridBagConstraints constraints=new GridBagConstraints();
        //使用GridBagLayout布局管理器
        frame.setLayout(gbaglayout);
        //组件填充显示区域
        constraints.fill=GridBagConstraints.BOTH;
        //恢复默认值
        constraints.weightx=2.5;
        //结束行
        constraints.gridwidth = GridBagConstraints.REMAINDER;

        textArea.setRows(3);
        textArea.setEnabled(false);
        textArea.setFont(new Font(null, 1, 16));
        textArea.setBackground(new Color(122, 134, 123));
        gbaglayout.setConstraints(textArea, constraints);
        frame.add(textArea);

        // 指定组件的分配区域
        constraints.weightx=0.5;
        constraints.weighty=0.2;
        constraints.gridwidth=1;

        for(int i = 0; i < buttonVaule.length; i ++){
            if((i+1) % 4 == 0){
                constraints.gridwidth=GridBagConstraints.REMAINDER;
            }
            String str = buttonVaule[i];
            buttons[i] = makeButton(str,frame,gbaglayout,constraints);
            if((i+1) % 4 == 0){
                constraints.gridwidth = 1;
            }
        }
        constraints.gridwidth=1;
        //设置容器大小
        frame.setBounds(400,400,400,400);
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}